﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharedComponents;

namespace OurLeague.Object
{
    public class Championship
    {
        #region Fields

        List<Team> _teams;
        List<Dayweek> _dayweeks;

        public static List<LeagueTableRecord> Statistics = new List<LeagueTableRecord>();

        #endregion

        #region Constructors

        public Championship()
        {
            _teams = new List<Team>();
            _dayweeks = new List<Dayweek>();
            
        }
        
        #endregion

        #region Public Methods

        /*
        public void insertGameweek(int a, DateTime b, DateTime c) { }

        public void insertMatch(String a, String b, String c, String d) { }

        public void insertLocalFormation(List<String> a, List<String> b, String c, String d) { }

        public void insertGuestFormation(List<String> a, List<String> b, String c, String d) { }

        public void insertEvent(String a, String b, String c, int d) { }

        public void inserTeam(String a, String b, String c, String d, int e) { }

        public void insertPlayer(List<String> playerAttributes) { }

        public void editDayweek(int a, DateTime b, DateTime c, String d) { }

        public void editMatch(String a, String b, DateTime c, String d, String e) { }

        public void editLocalFormation(List<String> a, List<String> b, String c, String d) { }

        public void editGuestFormation(List<String> a, List<String> b, String c, String d) { }

        public void editEvent(String a, String b, String c, int d, String e) { }

        public void editTeam(String a, String b, String c, int d, String e) { }

        public void editPlayer(List<String> playerAttributes) { }

        public void deleteDayweek(String a) { }

        public void deleteMatch(String a) { }

        public void deleteEvent(String a) { }

        public void deleteTeam(String a) { }

        public void deletePlayer(String a) { }

        public List<String> showDayweeks(String a)
        {
            return null;
        }

        public List<String> showTeams(String a)
        {
            return null;
        }

        public List<String> showPlayers(String a)
        {
            return null;
        }

        public List<String> showMatches(String a)
        {
            return null;
        }

        public List<String[]> leagueTable()
        {
            return null;
        }

        public double topScorers()
        {
            return 0;
        }

        public long minutesPlayed()
        {
            return 0;
        }
        */
        #endregion

        #region Properties

        #endregion
    }

    public class LeagueTableRecord : IComparable
    {
        String _teamName;

        int _win, _lost, _played, _draw, _score, _goalScored, _goalTaken, _goalDifference;

        public LeagueTableRecord(Team team)
        {
            Event.loadAll(team.Id);

            _teamName = team.Name;
            statistics(team);

            Championship.Statistics.Add(this);
        }

        #region Private Methods

        private void statistics(Team team)
        {
            var matches = Match.Matches.Where(m => m.GuestLineUp.TeamId == team.Id || m.LocalLineUp.TeamId == team.Id).ToList();
            foreach (Match m in matches)
            {
                //Event.loadAll(m.LocalLineUp.TeamId == team.Id ? m.GuestLineUp.TeamId : m.LocalLineUp.TeamId);

                int scoredGoals = m.LocalLineUp.TeamId == team.Id ? m.scoredGoals() : m.concededGoals();
                int concededGoals = m.GuestLineUp.TeamId == team.Id ? m.scoredGoals() : m.concededGoals();
                if (scoredGoals > concededGoals)
                    _win++;
                else
                    if (scoredGoals < concededGoals)
                        _lost++;
                    else
                        _draw++;
                _goalScored += scoredGoals;
                _goalTaken += concededGoals;
            }
            _goalDifference = _goalScored - _goalTaken;
            _played = _win + _lost + _draw;
            _score = _win * 3 + _draw;
        }

        #endregion

        #region Proprieties

        public int Position
        {
            get { return Championship.Statistics.FindIndex(s => s._teamName == _teamName) + 1; }
        }

        public String TeamName
        {
            get { return _teamName; }            
        }

        public int GoalDifference
        {
            get { return _goalDifference; }
        }

        public int GoalTaken
        {
            get { return _goalTaken; }
        }

        public int GoalScored
        {
            get { return _goalScored; }
        }

        public int Score
        {
            get { return _score; }
        }

        public int Draw
        {
            get { return _draw; }            
        }

        public int Played
        {
            get { return _played; }
        }

        public int Lost
        {
            get { return _lost; }
        }

        public int Win
        {
            get { return _win; }
        }

        #endregion

        #region IComparable Members

        public int CompareTo(object t)
        {
            LeagueTableRecord a = t as LeagueTableRecord;

            if (a._score > _score)
                return 1;
            else
                if (_score > a._score)
                    return -1;
                else
                    if (a._goalScored > _goalScored)
                        return 1;
                    else
                        if (_goalScored > a._goalScored)
                            return -1;
                        else
                            if (a._goalTaken < _goalTaken)
                                return 1;
                            else
                                if (_goalTaken < a._goalTaken)
                                    return -1;
                                else
                                    return a._teamName.CompareTo(_teamName);
        }

        #endregion
    }
}
